package com.zh.book_borrow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.zh.book_borrow.DTO.LoginDTO;
import com.zh.book_borrow.Exception.MyException;
import com.zh.book_borrow.common.Result;
import com.zh.book_borrow.entity.User;
import com.zh.book_borrow.service.UserService;
import com.zh.book_borrow.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginDTO loginDTO) throws MyException {
        log.info("loginDTO:{}",loginDTO);
        UserVO userVO = userService.login(loginDTO.getUsername(), loginDTO.getPassword());
        return Result.ok(userVO);
    }
    @SaCheckLogin
    @GetMapping("/info/{userId}")
    public Result<User> info(@PathVariable Long userId){
        log.info("userId:{}",userId);
        User user= userService.getInfo(userId);
        return Result.ok(user);
    }
    @SaCheckLogin
    @PostMapping("/logout")
    public Result<?> logout(){
        StpUtil.logout();
        return Result.ok();
    }
}
